<template>
  <div class="container">
    <div class="login-wrapper">
      <div class="header">Register</div>
      <form @submit.prevent="register">
        <div class="form-wrapper">
          <input type="text" name="username" v-model="username" placeholder="Username" class="input-item" @input="validateUsername">
          <input type="password" name="password" v-model="password" placeholder="Password" class="input-item" @input="validatePassword">
          <transition name="fade">
            <div>
            <span v-if="usernameError" class="error">{{ usernameError }}</span>
            <span v-if="passwordError" class="error">{{ passwordError }}</span>
            </div>
          </transition>
          <button type="submit" class="btn" :disabled="!isFormValid">注册</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts"> 
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const username = ref('')
const password = ref('')
const usernameError = ref('')
const passwordError = ref('')

// 正则表达式：允许英文、中文和数字，不允许为空
const usernameRegex = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/;
// 正则表达式：至少六位字符
const passwordRegex = /^.{6,}$/;

// 计算属性：检查用户名是否有效
const isUsernameValid = computed(() => {
  return username.value.trim() !== '' && username.value.match(usernameRegex) !== null;
});

// 计算属性：检查密码是否有效
const isPasswordValid = computed(() => {
  return password.value.trim() !== '' && password.value.match(passwordRegex) !== null;
});

// 计算属性：检查整个表单是否有效
const isFormValid = computed(() => {
  return isUsernameValid.value && isPasswordValid.value;
});

// 验证用户名
function validateUsername() {
  if (!isUsernameValid.value) {
    usernameError.value = '用户名格式不正确';
  } else {
    usernameError.value = '';
  }
}

// 验证密码
function validatePassword() {
  if (!isPasswordValid.value) {
    passwordError.value = '密码格式不正确';
  } else {
    passwordError.value = '';
  }
}

function register() {
  // 阻止表单的默认提交行为
  if (!isFormValid.value) {
    // 表单不合法，返回
    return;
  }

  // 登录成功，跳转到其他页面
  router.push({name:'login'})
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
}

body {
  height: 100%;
}

.container {
  height: 100vh;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}

.login-wrapper {
  background-color: #fff;
  width: 358px;
  height: 588px;
  border-radius: 15px;
  padding: 0 50px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}

.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}

.input-item::placeholder {
  text-transform: uppercase;
}

.btn {
  text-align: center;
  padding: 10px;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
}

.msg {
  text-align: center;
  line-height: 88px;
}

a {
  text-decoration-line: none;
  color: #abc1ee;
}

.error {
  font-size: 12px;
  color: red;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>